package com.qumitech.visualnovel.service.group.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.query.AccompanyAppraiseQueryAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.group.GroupDetailAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.group.GroupDetailAdminQueryDTO;
import com.qumitech.visualnovel.service.group.GroupService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/11/18 15:26
 */
@Service
public class GroupServiceImpl implements GroupService {

    private final String groupQueryURL = "/group/console/list";
    private final String groupAddURL = "/group/console";
    private final String groupRmURL = "/group/console/rm";
    private final String groupResetURL = "/group/console/reset";

    private final String groupIdItemURL = "/group/console/item";


    @Override
    public Map<String, Object> listGroupDetailAdminDTO(GroupDetailAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + groupQueryURL;
        Map<String, String> paramMap = new HashMap<>();

        if (queryDTO.getOffset() != null) {
            paramMap.put("offset", queryDTO.getOffset().toString());
        }
        if (queryDTO.getLimit() != null) {
            paramMap.put("limit", queryDTO.getLimit().toString());
        }
        if (queryDTO.getStartTime() != null) {
            paramMap.put("startTime", queryDTO.getStartTime().toString());
        }
        if (queryDTO.getEndTime() != null) {
            paramMap.put("endTime", queryDTO.getEndTime().toString());
        }
        if (queryDTO.getDeleteFlag() != null) {
            paramMap.put("delete_flag", queryDTO.getDeleteFlag().toString());
        }
        if (queryDTO.getGroupId() != null) {
            paramMap.put("group_id", queryDTO.getGroupId().toString());
        }
        if (queryDTO.getUid() != null) {
            paramMap.put("uid", queryDTO.getUid().toString());
        }
        if (queryDTO.getTargetId() != null) {
            paramMap.put("target_id", queryDTO.getTargetId().toString());
        }
        if (queryDTO.getId() != null) {
            paramMap.put("id", queryDTO.getId().toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);

        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addGroupDetailAdminDTO(GroupDetailAdminDTO dto) {
        dto.setTargetType((byte) 0);
        if (dto.getGroupId().equals("8") || dto.getGroupId().equals("9"))
        {
            dto.setTargetType((byte) 1);
        }

        if (StringUtils.isEmpty(dto.getUid())){
            dto.setUid(dto.getTargetId());
        }
        
        String url = UrlConstant.SERVER_URL + groupAddURL;
        dto.setOperator(getOperatorName());
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    @Override
    public void rmGroupDetailAdminDTO(Long id) {
        String url = UrlConstant.SERVER_URL + groupRmURL;
        String req = JSONObject.toJSONString(new GroupDetailAdminDTO(id,getOperatorName()));
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    @Override
    public void resetGroupDetailAdminDTO(Long id) {
        String url = UrlConstant.SERVER_URL + groupResetURL;
        String req = JSONObject.toJSONString(new GroupDetailAdminDTO(id,getOperatorName()));
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    private void checkBaseRsp( BaseRsp rsp){
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            if (rsp == null) {
                throw new BusinessException(ResultEnum.FAILURE);
            }
            Map<String, Object> rspMap = (Map<String, Object>) rsp.getError();
            ResultEnum resultEnum = ResultEnum.FAILURE;
            resultEnum.setMsg(rspMap.get("description").toString());
            resultEnum.setState(Integer.valueOf(rspMap.get("status").toString()));
            throw new BusinessException(resultEnum);
        }
    }
    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }


    @Override
    public BaseRsp listGroupIdItem() {
        GroupDetailAdminQueryDTO queryDTO=new GroupDetailAdminQueryDTO();
        String url = String.format("%s%s", UrlConstant.SERVER_URL, groupIdItemURL);
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

}
